package sopraim.model;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.SessionFactory;
import sopraim.entity.*;

public final class Model {
    
    //Singleton
    private volatile static Model single=null;
    // Status names and set
    private final Set <Status> status = new HashSet<Status>();
    private final static String[] STATUS_NAMES =
    {"available", "busy", "absent","offline"};
    
    //Composition of the DAO entities
    private final AdminDAO adminDAO;
    private final StatusDAO statutsDAO;
    private final ToolsDAO toolsDAO;
    private final UserDAO userDAO;
    private final TextMessageDAO textMessageDAO;
    private final  SessionFactory factory;
    
    /**
     * Constructor of Model, private because of the singleton pattern
     */
    private Model() {
        //Initializing the DAO objects
        toolsDAO = new ToolsDAO();
        factory= toolsDAO.getSessionFactory();
        
        adminDAO = new AdminDAO(factory);
        statutsDAO = new StatusDAO(factory);
        userDAO = new UserDAO(factory);
        textMessageDAO = new TextMessageDAO(factory);
        // toolsDAO.initDAO(statutsDAO, adminDAO, userDAO);
        initAllStatus() ;
        //Init the Database
        toolsDAO.initDAO(statutsDAO, adminDAO);
        // fill the Status list
        //TO DO : initialize the status
        
    }
    
    /**
     * Method returning an instance of Model
     * @return the instance of Model
     */
    public static Model getInstance(){
        if(single == null){
            synchronized(Model.class){
                if(single == null){
                    single = new Model();
                }
            }
        }
        return single;
    }
    
    public AdminDAO getAdminDAO() {
        return adminDAO;
    }
    
    public UserDAO getUserDAO() {
        return userDAO;
    }
    
    public StatusDAO getStatutsDAO() {
        return statutsDAO;
    }
    
    public TextMessageDAO getTextMessageDAO() {
        return textMessageDAO ;
    }
    public Status getStatus(String status_name) throws Exception {
        return statutsDAO.getStatus(status_name);
    }
    
    private void initAllStatus() {
        for (int i = 0; i < STATUS_NAMES.length ; i++) {
            Status s = new Status(STATUS_NAMES[i]);
            status.add(s); // add in model Class to local access
            // add in database to references with database
            try{
                statutsDAO.addStatus(s);
            }catch(Exception ex){
                
            }
            
        }
    }
    
}
